package utils

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"reflect"
)

func Result(code int, list interface{}, c *gin.Context) {
	switch code {
	case http.StatusOK:
		c.JSON(code, gin.H{
			"message": "操作成功",
			"data":    list,
			"code":    code,
		})
		return
	case http.StatusBadRequest:
		str := "操作失败"
		if list != nil {
			t := reflect.TypeOf(list)
			if t.Kind() == reflect.String {
				str = list.(string)
			}
		}

		c.JSON(http.StatusOK, gin.H{
			"message": str,
			"data":    list,
			"code":    code,
		})
		return
	}
}
